package com.lanou.moving.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.moving.R;
import com.lanou.moving.adapter.SingerMVAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.MVPlayRoot;
import com.lanou.moving.entity.SearchMvRoot;
import com.lanou.moving.entity.SingerMV;
import com.lanou.moving.media.MyMediaPlayer;
import com.lanou.moving.utils.Exchange;
import com.lanou.moving.volley.GsonRequest;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/27.
 */
public class MVPlayActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener, AdapterView.OnItemClickListener, SeekBar.OnSeekBarChangeListener {
    private String url;
    private MyMediaPlayer myMediaPlayer;
    private SeekBar mv_seekBar;
    private int videoId;
    private GsonRequest<SingerMV> mvGsonRequest;
    private String mv_url;
    private CheckBox mv_playOrPause;
    private TextView mv_currentTime, mv_maxTime, mv_detials_songName, mv_detials_singerName, mv_detials_playCountTv, mv_detials_commentCountTv;
    private String songName, singerName, playCount, commentCount, maxTime;
    private RelativeLayout mv_rl_control;
    private ImageView mv_fullScreen, center_play_img, mv_detials_back;
    private Handler handler;
    private List<SingerMV.DataEntity> mvs;
    private SingerMVAdapter adapter;
    private VolleySingleton volleySingleton;
    private ListView mv_listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mv_detials);
        BaseApplication.getInstance().addActivity(this);
        volleySingleton = VolleySingleton.getInstance(this);
        videoId = getIntent().getIntExtra("videoId", 0);
        url = "http://api.dongting.com/sim/mv/" +
                videoId +
                "/similarity?f=f62&v=v8.2.0.2015091720&utdid=VeKf29%2BKwt4DANT3MxDH4mCl&tid=0&mid=MI%2B4LTE";
        initView();
        getMvUrl();
        getMvDatas(url);
    }

    public void initView() {
        View headLayout = LayoutInflater.from(this).inflate(R.layout.mv_detials_listview_head, null);
        mv_detials_songName = (TextView) headLayout.findViewById(R.id.mv_detials_songName);//歌名
        mv_detials_singerName = (TextView) headLayout.findViewById(R.id.mv_detials_singerName);//歌手
        mv_detials_playCountTv = (TextView) headLayout.findViewById(R.id.mv_detials_playCountTv);//播放次数
        mv_detials_commentCountTv = (TextView) headLayout.findViewById(R.id.mv_detials_commentCountTv);//评论次数
        headLayout.setEnabled(false);
        mv_listview = (ListView) findViewById(R.id.mv_listview);
        mv_listview.addHeaderView(headLayout);
        mv_listview.setOnItemClickListener(this);
        mv_detials_back = (ImageView) findViewById(R.id.mv_detials_back);//返回
        mv_detials_back.setOnClickListener(this);
        mv_playOrPause = (CheckBox) findViewById(R.id.mv_playOrPause);//开始按钮
        mv_playOrPause.setOnCheckedChangeListener(this);
        center_play_img = (ImageView) findViewById(R.id.center_play_img);
        center_play_img.setOnClickListener(this);
        mv_seekBar = (SeekBar) findViewById(R.id.mv_seekBar);//进度条
        mv_currentTime = (TextView) findViewById(R.id.mv_currentTime);//进度时间
        mv_maxTime = (TextView) findViewById(R.id.mv_maxTime);//总时间
        mv_fullScreen = (ImageView) findViewById(R.id.mv_fullScreen);
        mv_fullScreen.setOnClickListener(this);
        mv_rl_control = (RelativeLayout) findViewById(R.id.mv_rl_control);//surface控制
        myMediaPlayer = (MyMediaPlayer) findViewById(R.id.my_mv_mediaPlayer);
        myMediaPlayer.setOnClickListener(this);
    }

    private void getMvUrl() {
        GsonRequest<MVPlayRoot> request = new GsonRequest<>(
                URL.URL_MV_PLAY_START + videoId + URL.URL_MV_PLAY_END, MVPlayRoot.class, new Response.Listener<MVPlayRoot>() {
            @Override
            public void onResponse(MVPlayRoot response) {
                mv_url = response.getData().getMvList().get(0).getUrl();
                if (mv_url != null) {
                    setPlay(mv_url);
                } else {
                    return;
                }
                songName = response.getData().getVideoName();
                singerName = response.getData().getSingerName();
                playCount = Exchange.getCount(response.getData().getPickCount());
                commentCount = Exchange.getCount(response.getData().getBulletCount());
                maxTime = Exchange.toTime(response.getData().getMvList().get(0).getDuration());
                mv_maxTime.setText(maxTime);
                mv_detials_songName.setText(songName);//歌名
                mv_detials_singerName.setText(singerName);//歌手名
                mv_detials_playCountTv.setText(playCount);//播放次数
                mv_detials_commentCountTv.setText(commentCount);//评论次数

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        volleySingleton.addQueue(request);
    }

    /**
     * 加载播放资源
     */
    public void setPlay(String url) {
        myMediaPlayer.setUrl(url);
        mv_seekBar.setMax(myMediaPlayer.getMax());
        mv_seekBar.setOnSeekBarChangeListener(this);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 1://刷新UI
                        mv_seekBar.setProgress(msg.arg1);
                        mv_currentTime.setText(Exchange.toTime(msg.arg1));
                        break;
                    case 2://让控制界面消失
                        mv_rl_control.setVisibility(View.GONE);
                        break;
                }
                return false;
            }
        });
        myMediaPlayer.setHandler(handler);
    }

    public void getMvDatas(String url) {
        mvGsonRequest = new GsonRequest<SingerMV>(url, SingerMV.class, new Response.Listener<SingerMV>() {
            @Override
            public void onResponse(SingerMV response) {
                mvs = response.getData();
                adapter = new SingerMVAdapter(MVPlayActivity.this, mvs);
                mv_listview.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(mvGsonRequest);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!myMediaPlayer.getMediaPlayer().isPlaying()) {
            myMediaPlayer.play();
            handler.sendEmptyMessageDelayed(2, 3000);
        } else {
            myMediaPlayer.pause();
            center_play_img.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_mv_mediaPlayer:
                mv_rl_control.setVisibility(View.VISIBLE);
                handler.removeMessages(2);
                handler.sendEmptyMessageDelayed(2, 3000);
                break;
            case R.id.mv_fullScreen:
                //让横屏显示,最大化
                myMediaPlayer.max();
                break;
            case R.id.center_play_img:
                myMediaPlayer.play();
                mv_playOrPause.isChecked();
                center_play_img.setVisibility(View.GONE);
                break;
            case R.id.mv_detials_back:
                this.finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        myMediaPlayer.stop();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position != 0) {
            int video_Id = (int) mvs.get(position - 1).getMvList().get(0).getVideoId();
            String url = "http://api.dongting.com/sim/mv/" +
                    video_Id +
                    "/similarity?f=f62&v=v8.2.0.2015091720&utdid=VeKf29%2BKwt4DANT3MxDH4mCl&tid=0&mid=MI%2B4LTE";
            mv_url = mvs.get(position - 1).getMvList().get(0).getUrl();
            getMvDatas(url);
            Log.i("Sysout", "MvPlayActivity->onItemClick");
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = mv_seekBar.getProgress();
        myMediaPlayer.seekTo(progress);
    }
}
